package com.yuelao.yuelao_backend.controller;

import com.yuelao.yuelao_backend.common.ApiResponse;
import com.yuelao.yuelao_backend.common.ErrorCode;
import com.yuelao.yuelao_backend.service.FileUploadService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 文件上传Controller
 */
@RestController
@RequestMapping("/upload")
@Slf4j
@RequiredArgsConstructor
public class FileUploadController {

    private final FileUploadService fileUploadService;

    /**
     * 上传图片
     * POST /upload/image
     * @param file 图片文件
     * @return 文件访问URL
     */
    @PostMapping("/image")
    public ApiResponse<String> uploadImage(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ApiResponse.error(ErrorCode.PARAMS_ERROR, "文件不能为空");
        }

        try {
            String fileUrl = fileUploadService.uploadFile(file, "image");
            return ApiResponse.ok(fileUrl);
        } catch (Exception e) {
            log.error("图片上传失败: {}", e.getMessage(), e);
            return ApiResponse.error(ErrorCode.SYSTEM_ERROR, "图片上传失败: " + e.getMessage());
        }
    }

    /**
     * 上传语音
     * POST /upload/voice
     * @param file 语音文件
     * @return 文件访问URL
     */
    @PostMapping("/voice")
    public ApiResponse<String> uploadVoice(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ApiResponse.error(ErrorCode.PARAMS_ERROR, "文件不能为空");
        }

        try {
            String fileUrl = fileUploadService.uploadFile(file, "voice");
            return ApiResponse.ok(fileUrl);
        } catch (Exception e) {
            log.error("语音上传失败: {}", e.getMessage(), e);
            return ApiResponse.error(ErrorCode.SYSTEM_ERROR, "语音上传失败: " + e.getMessage());
        }
    }

    /**
     * 上传视频
     * POST /upload/video
     * @param file 视频文件
     * @return 文件访问URL
     */
    @PostMapping("/video")
    public ApiResponse<String> uploadVideo(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return ApiResponse.error(ErrorCode.PARAMS_ERROR, "文件不能为空");
        }

        try {
            String fileUrl = fileUploadService.uploadFile(file, "video");
            return ApiResponse.ok(fileUrl);
        } catch (Exception e) {
            log.error("视频上传失败: {}", e.getMessage(), e);
            return ApiResponse.error(ErrorCode.SYSTEM_ERROR, "视频上传失败: " + e.getMessage());
        }
    }

    /**
     * 删除文件
     * DELETE /upload/file
     * @param fileUrl 文件URL
     * @return 是否成功
     */
    @DeleteMapping("/file")
    public ApiResponse<Boolean> deleteFile(@RequestParam("fileUrl") String fileUrl) {
        try {
            boolean result = fileUploadService.deleteFile(fileUrl);
            return ApiResponse.ok(result);
        } catch (Exception e) {
            log.error("文件删除失败: {}", e.getMessage(), e);
            return ApiResponse.error(ErrorCode.SYSTEM_ERROR, "文件删除失败: " + e.getMessage());
        }
    }

}